package cn.mesmile.nacos.model;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.mesmile.nacos.dto.Datas;
import cn.mesmile.nacos.dto.Series;
import cn.mesmile.nacos.dto.StatisticalResults;
import cn.mesmile.nacos.dto.XAxis;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author zb
 * @date 2021/6/18 23:40
 * @Description: 今日数据
 */
public class TodayData extends Strategy {


    @Override
    StatisticalResults getStatisticsResult(List<Datas> dataList) {
        // todo 获取所有老用户id 放在此变量中
        Set<String> oldUserId = new HashSet<>();
        oldUserId.add("123456");

        int hour = DateUtil.hour(new Date(), true);
        List<String> dataX = new ArrayList<>();
        List<Integer> dataYScanning = new ArrayList<>(hour);
        List<Integer> oldUserY = new ArrayList<>(hour);
        List<Integer> newUserY = new ArrayList<>(hour);
        List<Integer> numberOfPeopleY = new ArrayList<>(hour);
        // 组装 x 轴数据 和 初始化 y 轴的值
        for (int i = 0; i <= hour; i++) {
            dataX.add(i+":00~"+(i+1)+":00");
            dataYScanning.add(0);
            oldUserY.add(0);
            newUserY.add(0);
            numberOfPeopleY.add(0);
        }
        XAxis xAxis = new XAxis();
        xAxis.setData(dataX);

        List<Series> seriesList = new ArrayList<>(4);
        ////////////////扫码次数///////////////////
        Series scanningTimes = new Series();
        scanningTimes.setName("扫码次数");
        ////////////////老用户数///////////////////
        Series oldUser = new Series();
        oldUser.setName("老用户数");
        ////////////////新用户数///////////////////
        Series newUser = new Series();
        newUser.setName("新用户数");
        ////////////////老用户数///////////////////
        Series numberOfPeople = new Series();
        numberOfPeople.setName("曝光总人数");


        // key 为整点时间 ， value 为次数
        Map<Integer, List<Datas>> data = dataList.stream().collect(Collectors.groupingBy(series ->
                DateUtil.hour(series.getCreateDate(), true))
        );
        Iterator<Map.Entry<Integer, List<Datas>>> iterator = data.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<Integer, List<Datas>> next = iterator.next();
            // x轴下标
            Integer key = next.getKey();
            // 次数
            List<Datas> value = next.getValue();
            // 时段的总次数
            int totalSize = value.size();
            // 老用户扫码次数
            long oldUserCount = value.stream().filter(datas -> oldUserId.contains(datas.getSharedCustomerId())).count();
            // 根据扫码人id去重
            long count = value.stream().map(Datas::getSharedCustomerId).distinct().count();
            dataYScanning.set(key, totalSize);
            oldUserY.set(key, Convert.toInt(oldUserCount));
            newUserY.set(key, Convert.toInt(totalSize - oldUserCount));
            numberOfPeopleY.set(key, Convert.toInt(count));
        }
        scanningTimes.setData(dataYScanning);
        oldUser.setData(oldUserY);
        newUser.setData(newUserY);
        numberOfPeople.setData(numberOfPeopleY);

        seriesList.add(scanningTimes);
        seriesList.add(oldUser);
        seriesList.add(newUser);
        seriesList.add(numberOfPeople);

        return new StatisticalResults(seriesList, xAxis);
    }
}
